package orest.front.membercenter;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import orest.back.membermanage.RegisterUserHandler;
import orest.front.market.MarketHandler;

import net.ssta.common.Constants;
import net.ssta.common.DataBaseConfig;
import net.ssta.common.LoginUser;
import net.ssta.core.base.BaseException;
import net.ssta.core.base.ErrorCode;
import net.ssta.core.base.ServletBase;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.core.util.DateUtility;
import net.ssta.core.web.RequestKit;

import com.yztc.rc.api.framework.entity.User;
import com.yztc.rc.api.framework.entity.UserAccount;
import com.yztc.rc.api.framework.manager.UserManager;

public class AchieveTrackServlet extends ServletBase {

	
	private static final long serialVersionUID = 1L;
	 

	public void defaultAction(HttpServletRequest request,HttpServletResponse response) 
	{
		
	}
	
	/**
	 * 添加成果跟踪信息
	 * */
	public void addTrack(HttpServletRequest request,HttpServletResponse response)
    {
    	Connection conn = null;
    	RequestKit requestKit=new RequestKit(request);
    	String forwardurl = "";
    	
    	try {
    		//获取数据库链接
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
    	
	    	//从页面上获取技术需求相关内容
	    	Record record = requestKit.getRecordWithPrefix("TRACK_");
	    	
	    	//添加用户信息表
	    	record.setEntityName("T_ACHIEVETRACK");
	    	record.setConnection(conn);
	    	record.setPrimaryKey("ID");
	    	
	    	record.addData("SUBMITDATE", DateUtility.getCurrentDateTime());//提交日期为当前日期
	    	record.addData("USERID", LoginUser.getUserId()); 		//获取登录人员uuid
	    	record.addData("IFCHECK", "0");		//是否通过审核，默认设置为0，未审核。控制机构用户发布的信息不能随意在前台展示
	    	
	    	//数据库中插入技术需求信息
	    	Record recordresult = record.create();
	    	
	    	//添加成果返回成果列表页面
	    	response.sendRedirect(request.getContextPath()+"/membercenter/achievetrack?method=achieveList");
	    	//this.sendRedirectByStyle(request.getContextPath()+"/membercenter/achievetrack?method=achieveList");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch(SQLException s){
			s.printStackTrace();
		}finally{
			//释放数据库连接
			if (conn != null)
	        {
				try
	            {
	                conn.close();
	            }catch (SQLException e){
	                e.printStackTrace();
	            }
	        }
		}	
    }
	
	/**
	 * 修改成果跟踪信息
	 * **/
	public void updateTrack(HttpServletRequest request,HttpServletResponse response)
    {
    	Connection conn = null;
    	RequestKit requestKit=new RequestKit(request);
    	String forwardurl = "";
    	
    	try {
    		//获取数据库链接
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
    		
    		//从页面上获取成果跟踪信息ID
    		String trackid = requestKit.getParameter("uuid", "");
	    	//从页面上获取技术需求相关内容
	    	Record record = requestKit.getRecordWithPrefix("TRACK_");
	    	
	    	//修改投融资项目
	    	record.setEntityName("T_KJCGGZ");
	    	record.setConnection(conn);
	    	record.setPrimaryKey("ID");
	    	record.addData("ID", trackid);
	    	
	    	record.addData("SUBMITDATE", DateUtility.getCurrentDateTime());//提交日期为当前日期
	    	record.addData("USERID", LoginUser.getUserId()); 		//获取登录人员id
	    	record.addData("IFCHECK", "0");		//将审核标志位设置为未审核。其他标志位不变。
	    	
	    	//数据库中修改投融资项目信息
	    	record.update();
	    	
	    	response.sendRedirect(request.getContextPath()+"/membercenter/market?method=techneedList");
	    	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch(SQLException s){
			s.printStackTrace();
		}finally{
			//释放数据库连接
			if (conn != null)
	        {
				try
	            {
	                conn.close();
	            }catch (SQLException e){
	                e.printStackTrace();
	            }
	        }
		}
    }
	
	/**
	 * 删除成果跟踪信息
	 * **/
	public void deleteTrack(HttpServletRequest request,HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        AchieveTrackHandler trackhandler = new AchieveTrackHandler();
        
        String trackid = requestKit.getParameter("trackid", "");

        trackhandler.deleteTrack(requestKit);
    }
	
	/**
	 * 成果跟踪信息列表
	 * **/
	public void achieveList(HttpServletRequest request,HttpServletResponse response)
    {
    	
    	try {
			
			RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
	        PagingInfo pagingInfo = new PagingInfo(request, 10, "^(?!.*(result)|(LTPAToken)).*$");
	        
	        AchieveTrackHandler trackhandler = new AchieveTrackHandler();
	        String userid =LoginUser.getUserId();//当前登录人uuid
	        
	        RecordSet achieveSet = trackhandler.unitAchieveList(userid,pagingInfo);//获取成果列表
	        Map trackcount = trackhandler.achieveTrackCount(userid);	//获取成果对应的跟踪信息数量
	        
	        request.setAttribute("achieveSet", achieveSet);
	        request.setAttribute("trackcount", trackcount);
	        request.setAttribute("pagingInfo", pagingInfo);
	        this.forwardByStyle("/membercenter/achievetrack/achieveList.jsp");
	    	//forward("/membercenter/achievetrack/achieveList.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException ex){
			ex.printStackTrace();
		}
    }
	
	/**
	 * 指定成果的跟踪信息列表，跟踪信息列表不翻页
	 * **/
	public void trackList(HttpServletRequest request,HttpServletResponse response)
    {
    	
    	try {
			
			RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
	        
	        AchieveTrackHandler trackhandler = new AchieveTrackHandler();
	        String userid =LoginUser.getUserId();//当前登录人uuid
	        String kjcgid = request.getParameter("kjcgid");//科技成果id
	        
	        RecordSet trackSet = trackhandler.unitAchieveTrackList(userid,kjcgid);//获取成果列表
	        request.setAttribute("trackSet", trackSet);
	        String achievetitle = "";
	        if(trackSet!=null && trackSet.size()>0){
	        	Record record = trackSet.get(0);
	        	achievetitle = record.getString("KJCG_ITEM_3","");
	        }
	        request.setAttribute("achievetitle", achievetitle);
	    	//forward("/membercenter/achievetrack/trackList.jsp");
	        this.forwardByStyle("/membercenter/achievetrack/trackList.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException ex){
			ex.printStackTrace();
		}
    }
	
}
